package example.player

import kotlin.properties.Delegates

class Player {

    var stateListener: OnPlayStateListener? = null

    var state: State by Delegates.observable(State.IDLE) { property, oldValue, newValue ->
        println("状态发生变化，初始化状态：${property}，旧状态：$oldValue，当前状态:$newValue")

        //通知状态
        stateListener?.onChange(oldValue, newValue)
    }


    fun sendCmd(playCmd: PlayCmd) {

        when (playCmd) {
            is Play -> doPlay(playCmd.url, playCmd.position)//播放
            is Seek -> doSeekTo(playCmd.position)//跳跃
            is Resume -> doResume()//继续播放
            is Pause -> doPause()//暂停播放
            is Stop -> doStop()//停止播放
        }

    }

    private fun doPlay(url: String, position: Long) {
        println("开始播放 url:$url，位置:$position")
        state = State.PALYING
    }

    private fun doSeekTo(position: Long) {
        println("跳跃播放 位置:$position")
        state = State.PALYING
    }

    private fun doResume() {
        println("继续播放")
        state = State.PALYING
    }

    private fun doPause() {
        println("暂停播放")
        state = State.PAUSE
    }

    private fun doStop() {
        println("停止播放")
        state = State.IDLE
    }

    /**
     * 播放状态监听
     */
    interface OnPlayStateListener {
        fun onChange(oldState: State, newState: State)
    }
}